
/**
 * 用途         :常用UI,字符串,数组通用方法,基本上自制方法都在这里
 * 开发者       :郑品延
 * 修改时间     :2019/8/6
 */
export default class JayUtil{
    public static platform = '';
    public static setPlatform() { //暂时只做简单判断，wx/other
        if (cc.sys.platform == cc.sys.MACOS) {
            this.platform = "macOs";
        }
        else if (cc.sys.platform == cc.sys.ANDROID) {
            this.platform = "android";
        }
        else if (cc.sys.platform == cc.sys.IPHONE) {
            this.platform = "iphone";
        }
        else if (cc.sys.platform == cc.sys.IPAD) {
            this.platform = "ipad";
        }
        else if (cc.sys.platform == cc.sys.MOBILE_BROWSER) {
            this.platform = "mobileBrowser";//手机流浪器
        }
        else if (cc.sys.platform == cc.sys.DESKTOP_BROWSER) {
            this.platform = "desktopBrowser";//桌面流浪器
        }
        else if (cc.sys.platform == cc.sys.WECHAT_GAME) {
            if (cc.sys.browserType == cc.sys.BROWSER_TYPE_WECHAT_GAME) {
                this.platform = "weChatGame";
            }
            else {
                this.platform = "weChat";
            }
        }
        else if (cc.sys.platform == cc.sys.QQ_PLAY) {
            if (cc.sys.browserType == cc.sys.BROWSER_TYPE_QQ_PLAY) {
                this.platform = "qqPlay";
            }
        } 
        else if (cc.sys.platform == cc.sys.BAIDU_GAME) {
            this.platform = "baiduGame";
        } 
        else if (cc.sys.platform == cc.sys.OPPO_GAME) {
            this.platform = "oppoGame";
        }
        else if (cc.sys.platform == cc.sys.VIVO_GAME) {
            this.platform = "vivoGame";
        }
        else {
            this.platform = "macOs";
        }
        Log("cc.sys.platform:", cc.sys.platform)
        Log("当前运行平台是", this.platform)
    }

    public static getPlatform() {
        return this.platform;
    }
    //------------------------------------------------------------------------------------------------
    public static convertToTargetNodeSpace (srcNode, destNode) {
        let worldPos = srcNode.convertToWorldSpaceAR(cc.v2(0, 0));
        let nodePost = destNode.convertToNodeSpaceAR(worldPos);
        return nodePost;
    }

    public static isChinese () {
        var currentLanguageType = cc.sys.language;
        if (currentLanguageType == cc.sys.LANGUAGE_CHINESE) {
            return true;
        }
        return false;
    }

    //控制UI最大宽度(通过缩放UI实现)        //高模式
    public static scaleUIMaxWidthByWin (uiNode, offsetWidth) {
        if (typeof (offsetWidth) == 'undefined') {
            offsetWidth = 50;
        }
        let winWidth = cc.winSize.width;
        let maxWidth = winWidth - offsetWidth;
        if (uiNode.width > maxWidth) {
            let scale = maxWidth / uiNode.width;
            uiNode.scale = scale;
        }
    }
    //控制UI最大高度(通过缩放UI实现)        //宽模式
    public static scaleUIMaxHeightByWin (uiNode, offsetHeight) {
        if (typeof (offsetHeight) == 'undefined') {
            offsetHeight = 50;
        }
        let winHeight = cc.winSize.height;
        let maxHeight = winHeight - offsetHeight;
        if (uiNode.height > maxHeight) {
            let scale = maxHeight / uiNode.height;
            uiNode.scale = scale;
        }
    }
    //处理多过长的文字内容，过长的内容用省略号代替
    public static longMsgUseEllipsis (msg, maxWidth, fontSize) {

        let msgNode = new cc.Node();
        msgNode.x = 10000;
        let msgLabel = msgNode.addComponent(cc.Label);
        msgLabel.fontSize = fontSize;
        cc.director.getScene().addChild(msgNode);
        msgLabel.string = msg;
        let curWidth = msgNode.width;
        while (curWidth > maxWidth) {
            let offsetPercent = (curWidth - maxWidth) / curWidth;
            let offsetLength = Math.ceil(msg.length * offsetPercent);
            msg = msg.substring(0, msg.length - offsetLength);
            msgLabel.string = msg;
            curWidth = msgNode.width;
        };
        msgNode.destroy();
        // return msg + '..';
        return msg;
    }

    public static isHaveChildByName (parent, childName) {
        let childNode = parent.getChildByName(childName);
        if (childNode) {
            return true;
        }
        return false;
    }

    //拆分字符串
    public static splitStringToNumberArray(str, fuhao) {
        var array = str.split(fuhao);
        var newArray = [];
        for (var i = 0; i < array.length; i++) {
            newArray.push(array[i]);
        }
        return newArray;
    }
    public static getObjectLength(obj) {
        return Object.keys(obj).length;
    }
    //宽屏进行坐标的缩放
    public static scalePosition(node) {
        var winWidth = cc.winSize.width;
        var designWidth = 750;  //设计分辨率
        if (winWidth > designWidth) {
            var scale = winWidth / designWidth;
            node.x = node.x * scale;
        }
    }
    public static NumberToChinese(num) {
        var unitPos = 0;
        var strIns = '', chnStr = '';
        var needZero = false;

        var chnNumChar = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"];
        var chnUnitSection = ["", "万", "亿", "万亿", "亿亿"];
        var chnUnitChar = ["", "十", "百", "千"];
        function SectionToChinese(section) {
            var strIns = '', chnStr = '';
            var unitPos = 0;
            var zero = true;
            while (section > 0) {
                var v = section % 10;
                if (v === 0) {
                    if (!zero) {
                        zero = true;
                        chnStr = chnNumChar[v] + chnStr;
                    }
                } else {
                    zero = false;
                    strIns = chnNumChar[v];
                    strIns += chnUnitChar[unitPos];
                    chnStr = strIns + chnStr;
                }
                unitPos++;
                section = Math.floor(section / 10);
            }
            return chnStr;
        }

        if (num === 0) {
            return chnNumChar[0];
        }

        while (num > 0) {
            var section = num % 10000;
            if (needZero) {
                chnStr = chnNumChar[0] + chnStr;
            }
            strIns = SectionToChinese(section);
            strIns += (section !== 0) ? chnUnitSection[unitPos] : chnUnitSection[0];
            chnStr = strIns + chnStr;
            needZero = (section < 1000) && (section > 0);
            num = Math.floor(num / 10000);
            unitPos++;
        }

        return chnStr;
    }

    public static showLayer(node) {
        node.active = true;
        var rootNode = node.getChildByName('RootNode');
        if (rootNode) {
            rootNode.scale = 0;
            rootNode.runAction(cc.scaleTo(0.3, 1));
            // rootNode.opacity = 0;
            // rootNode.runAction(cc.fadeIn(0.5))
        }
    }
    public static closeLayer(node) {
        var rootNode = node.getChildByName('RootNode');
        if (rootNode) {
            var seq = cc.sequence(cc.scaleTo(0.1, 1.1), cc.scaleTo(0.3, 0), cc.callFunc(() => { node.destroy() }))
            rootNode.runAction(seq);
        }
        else {
            node.destroy();
        }
    }
    //随机打乱数组
    public static shuffle(b) {
        var a = [].concat(b);
        var len = a.length;
        for (var i = 0; i < len; i++) {
            var end = len - 1;
            var index = (Math.random() * (end + 1)) >> 0;
            var t = a[end];
            a[end] = a[index];
            a[index] = t;
        }
        return a;
    }


    
}
